package org.geekbang.thinking.in.spring.ioc.overview.dependency.lookup;

import org.geekbang.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * lookup
 *
 * @author liucheng
 * @since 0.1 2021-01-10 2:57 下午
 **/
public class DependencyLookupDemo {
    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-lookup-context.xml");
//        User user = getUserRealTime(beanFactory);
        User user = getUserLazy(beanFactory);
        System.out.println(user);
    }

    private static User getUserLazy(BeanFactory beanFactory) {
        ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean("objectFactory");
        return objectFactory.getObject();
    }

    private static User getUserRealTime(BeanFactory beanFactory) {
        return (User) beanFactory.getBean("user");
    }
}
